package la.iok.hzsvn.lewin.movie.cloud.controller;

import la.iok.hzsvn.lewin.movie.cloud.model.*;
import la.iok.hzsvn.lewin.movie.cloud.service.DashBoardService;
import la.iok.hzsvn.lewin.movie.cloud.service.UserService;
import la.iok.hzsvn.lewin.movie.core.Token;
import la.iok.hzsvn.lewin.movie.core.model.NicknameParam;
import la.iok.hzsvn.lewin.movie.core.model.UserVo;
import la.iok.hzsvn.lewin.mybatis.model.PageVo;
import la.iok.hzsvn.share.response.ListDataResponse;
import la.iok.hzsvn.share.response.OperatorResponse;
import la.iok.hzsvn.share.response.OperatorResponses;
import la.iok.hzsvn.share.response.SingleDataResponse;
import la.iok.hzsvn.share.utils.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("users")
public class UserController {
    private final UserService userService;
    private final DashBoardService dashBoardService;

    public UserController(UserService userService,
                          DashBoardService dashBoardService) {
        this.userService = userService;
        this.dashBoardService = dashBoardService;
    }

    @PostMapping
    public OperatorResponse save(@RequestHeader(required = false) Token token,@RequestBody UserParam user){
        if(token != null){
            //管理员添加用户
            if(StringUtils.isBlank(user.getPassword())){
                user.setPassword("88888888");
            }
            userService.save(token,user);
        }else{
            userService.qrCodeSave(user);
        }
        return OperatorResponses.success();
    }
    @PutMapping("{id}")
    public OperatorResponse update(@RequestHeader Token token,@PathVariable Long id,@RequestBody UserModifyParam user){
        userService.modify(token,id,user);
        return OperatorResponses.success();
    }

    @PutMapping("{id}/defaultPwd")
    public OperatorResponse resetPwd(@RequestHeader Token token,@PathVariable Long id){
        userService.resetPwd(token,id);
        return OperatorResponses.success();
    }

    @PutMapping("account")
    public OperatorResponse modifyAccount(@RequestHeader Token token, @RequestBody AccountModifyParam account){
        userService.modifyAccount(token,account);
        return OperatorResponses.success();
    }
    @PutMapping("password")
    public OperatorResponse modifyPassword(@RequestHeader Token token,@RequestBody PasswordModifyParam param){
        userService.modifyPassword(token,param);
        return OperatorResponses.success();
    }

    /**
     * 忘记密码时重置密码
     * @param param 密码重置参数
     * @return 操作结果
     */
    @PutMapping("resetPassword")
    public OperatorResponse forgotPassword(@RequestBody ForgotSetPasswordParam param){
        userService.modifyPassword(param);
        return OperatorResponses.success();
    }

    @PostMapping("token")
    public SingleDataResponse<TokenInfo> token(@RequestBody UserLoginParam param){
        return new SingleDataResponse<>(userService.token(param));
    }
    @GetMapping("info")
    public SingleDataResponse<UserVo> info(@RequestHeader Token token){
        return new SingleDataResponse<>(userService.po2vo(userService.select(token.getUserId())));
    }
    @GetMapping
    public SingleDataResponse<PageVo<UserVo>> page(@RequestHeader Token token, int page, int limit, Long orgUnitId, String phoneNumber){
        page = Math.max(1,page);
        limit = Math.min(Math.max(1,limit),50);
        PageVo<UserVo> pageRecords = userService.page(page,limit,token,orgUnitId,phoneNumber);
        return new SingleDataResponse<>(pageRecords);
    }

    /**
     * 获取用户总数
     * @param token 操作用户
     * @param orgUnitId 查询的组织id.比如查询某个加盟商所有店铺的用户总数。可能为null
     * @return 查询的用户数量。根据操作用户的权限统计。如果是某个加盟商的用户,那就只能看那个加盟商下所有店铺的用户数
     */
    @GetMapping("total")
    public SingleDataResponse<Integer> total(@RequestHeader Token token,Long orgUnitId){
        return new SingleDataResponse<>(dashBoardService.userTotalCount(token,orgUnitId));
    }

    /**
     * 当天活跃用户数量统计
     * @return 活跃用户统计
     */
    @GetMapping("todayActiveUser")
    public ListDataResponse<AccessCount> todayActiveUser(){
        return new ListDataResponse<>(userService.todayActiveUser());
    }

    @PutMapping("nickname")
    public OperatorResponse nickname(@RequestHeader Token token, @RequestBody NicknameParam nickname){
        userService.nickname(token,nickname.getNickname(),nickname.getGender());
        return OperatorResponses.success();
    }

    @PutMapping("{id}/enable")
    public OperatorResponse enable(@RequestHeader Token token, @PathVariable Long id){
        userService.enable(token,id);
        return OperatorResponses.success();
    }

    @PutMapping("{id}/disable")
    public OperatorResponse disable(@RequestHeader Token token, @PathVariable Long id){
        userService.disable(token,id);
        return OperatorResponses.success();
    }
}
